package com.allenlee.mvphelper.module.login;

import android.Manifest;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;

import com.allenlee.mvphelper.R;
import com.allenlee.mvphelper.base.BaseFragment;
import com.allenlee.mvphelper.manager.PreferencesManager;
import com.allenlee.mvphelper.module.testinfo.TestInfoFragment;
import com.qmuiteam.qmui.util.QMUIDirection;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.qmuiteam.qmui.util.QMUIViewHelper;

import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatImageView;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author : lifei
 * 创建时间： 19-11-27
 * 更改时间： 19-11-27
 * Email : allen.lee723@qq.com
 * @version :
 * Description：
 */
public class LoginFragment extends BaseFragment<LoginPresenter> implements LoginContract.View {

    @BindView(R.id.fragment_login_clientid_et)
    AppCompatEditText mClientIdEt;
    @BindView(R.id.fragment_login_password_et)
    EditText mPasswordEt;
    @BindView(R.id.fragment_login_save_password_cb)
    CheckBox mSavePasswordCb;
    @BindView(R.id.fragment_login_top_bg)
    AppCompatImageView mTopBgIv;

    @Override
    protected void beforeInitView() {

    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_login;
    }

    @Override
    protected void initTopbar() {

    }

    @Override
    protected void initView() {
        if (mTopBgIv.getVisibility() == View.GONE) {
            QMUIViewHelper.slideIn(mTopBgIv, 300, null, true, QMUIDirection.TOP_TO_BOTTOM);
        }

        Account account = PreferencesManager.getInstance().getAccount();
        mSavePasswordCb.setChecked(account.isSaved());
        if (account.isSaved()) {
            mClientIdEt.setText(account.getClientId());
            mPasswordEt.setText(account.getPassword());
        } else {
            mClientIdEt.setText("");
            mPasswordEt.setText("");
        }
    }

    @Override
    protected LoginPresenter setPresenter() {
        return new LoginPresenter();
    }

    @Override
    protected void afterSetPresenter() {

    }

    @Override
    public void onResume() {
        super.onResume();
        QMUIStatusBarHelper.setStatusBarLightMode(mActivity);
    }

    @Override
    public void onStop() {
        super.onStop();
        QMUIStatusBarHelper.setStatusBarDarkMode(mActivity);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (!isSaveAccount()) {
            // 保存用户信息
            PreferencesManager.getInstance().saveAccount(
                new Account(getClientId(), getPassword(), isSaveAccount()));
        }
    }

    @OnClick(R.id.fragment_login_login_bt)
    public void onViewClick(View view) {
        switch (view.getId()) {
            case R.id.fragment_login_login_bt:
                // 登录时，请求 READ_PHONE_STATE 权限
                requestPermissions(Manifest.permission.READ_PHONE_STATE);
                break;
        }
    }

    @Override
    public String getClientId() {
        return mClientIdEt.getText().toString().trim();
    }

    @Override
    public String getPassword() {
        return mPasswordEt.getText().toString().trim();
    }

    @Override
    public boolean isSaveAccount() {
        if (mSavePasswordCb != null) {
            return mSavePasswordCb.isChecked();
        }
        return false;
    }

    @Override
    public void permissionGranted() {
        // 权限请求成功才允许登录
        mPresenter.getLoginInfo();
        //startFragment(new TestInfoFragment());
    }

    @Override
    public void onLoginSuccess() {
        // 保存用户信息
        PreferencesManager.getInstance().saveAccount(
            new Account(getClientId(), getPassword(), isSaveAccount()));

        startFragmentAndDestroyCurrent(new TestInfoFragment());
    }
}